//
//  GouMaiLiuChengCollectionViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/5/12.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GouMaiLiuChengCollectionViewController.h"
#import "cpliuchengCell.h"
#import "cpliuchengCell2.h"
#import "ProductViewController.h"

@interface GouMaiLiuChengCollectionViewController ()<gotobuygood>

//底部pagecontrol
@property (nonatomic,retain) UIPageControl *pagecontrol;
//每张图片显示标题数组
@property (nonatomic,retain) NSArray *textarray;

//每张图片显示详情数组
@property (nonatomic,retain) NSArray *detailarray;


@end

@implementation GouMaiLiuChengCollectionViewController


static NSString * const reuseIdentifier = @"Cell";
static NSString * const reuseIdentifier2 = @"Cell2";

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据
    [self addData];
  //   self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.frame = CGRectMake(0, 0 * kAdjustParamater, kScreenWidth, kScreenHeight);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[cpliuchengCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[cpliuchengCell2 class] forCellWithReuseIdentifier:reuseIdentifier2];
 //   [self.collectionView addSubview:_pagecontrol];
    
  
    // 布局
    [self creatpagecontrol];
    
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"购买流程";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 添加数据
- (void)addData
{
    //产品流程页面的大标题
    _textarray = [[NSArray alloc]initWithObjects:@"1.下单购买。",@"2.样品采集。",@"3.基因检测。",@"4.查看报告。", nil];
    //产品流程页面的详细描述
    _detailarray = [[NSArray alloc]initWithObjects:@"请您在APP/微信/网站上进行注册，下单购买产品。",@"请您按照采样器说明手册指引，采集唾液，绑定单号，回寄样本。详细采样流程请点击查看。",@"我们将利用最先进科技手段，对您的基因进行精准检测，并生成健康评估报告。",@"回寄样本3周后，您可在APP/微信/网站上查看电子报告，期间也可随时登录跟踪订单进程。", nil];
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatpagecontrol{
    _pagecontrol = [[UIPageControl alloc]initWithFrame:CGRectMake((375 - 100) / 2 * kAdjustParamater, (kScreenHeight - 85), 100 * kAdjustParamater, 10 * kAdjustParamater)];
  // _pagecontrol.center = CGPointMake(self.view.center.x,self.view.bounds.size.height - 10 * kAdjustParamater);
//    _pagecontrol.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _pagecontrol.numberOfPages = _textarray.count + 1;
    _pagecontrol.currentPageIndicatorTintColor = kCyanColor;
    _pagecontrol.pageIndicatorTintColor = [UIColor grayColor];
    _pagecontrol.tag = 100;
    [self.view addSubview:_pagecontrol];
}

- (NSIndexPath *)curIndexPath {
    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *curIndexPath = nil;
    NSInteger curzIndex = 0;
    for (NSIndexPath *path in indexPaths.objectEnumerator) {
        UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
        if (!curIndexPath) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
            continue;
        }
        if (attributes.zIndex > curzIndex) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
        }
    }
    return curIndexPath;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (indexPath.row == curIndexPath.row) {
        return YES;
    }
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 1) {
//        [self presentViewController:[[NavViewController alloc]initWithRootViewController:[detailliuc new]] animated:YES completion:nil];
//    }
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _textarray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != 4) {
        cpliuchengCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"cplc%ld", indexPath.row + 1]];
        cell.label.text = _textarray[indexPath.row];
        cell.detaillabel.text = _detailarray[indexPath.row];
        return cell;
    }else{
        cpliuchengCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier2 forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"cplc%ld", indexPath.row + 1]];
        cell.delegate = self;
        return cell;
    }
}
//设置跳转
- (void)gotobuy{
    NSLog(@"购买产品");
    ProductViewController *productVC = [[ProductViewController alloc] init];
    productVC.isJump = YES;
    [self.navigationController pushViewController:productVC animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    UIPageControl *pagecontrol = (UIPageControl *)[self.view viewWithTag:100];
    if (scrollView.contentOffset.x < self.view.bounds.size.width / 2) {
        pagecontrol.currentPage = 0;
    }else if (kScreenWidth / 2 <= scrollView.contentOffset.x && scrollView.contentOffset.x < kScreenWidth * 1.5){
        pagecontrol.currentPage = 1;
    }else if (kScreenWidth * 1.5 <= scrollView.contentOffset.x && scrollView.contentOffset.x < kScreenWidth * 2.5){
        pagecontrol.currentPage = 2;
    }else if (kScreenWidth * 2.5 <= scrollView.contentOffset.x && scrollView.contentOffset.x < kScreenWidth * 3.5){
        pagecontrol.currentPage = 3;
    }else{
        pagecontrol.currentPage = 4;
    }
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
